﻿using System.Drawing;
using System.Windows.Forms;
using System;


namespace AlphaMobileControls
{
	/// <summary>
	/// Simple PictureBox control handling alpha channel.
	/// </summary>
	public class AlphaPictureBox : AlphaControl
	{
		private AlphaImage _image;
		private uint _alpha = 0;

        /// <summary>
        /// The image to draw.
        /// </summary>
		public AlphaImage Image
		{
			get { return _image; }
			set
			{
				_image = value;
				if (_image != null)
					_image.Alpha = _alpha;
			}
		}

        /// <summary>
        /// The Alpha channel for the image.
        /// </summary>
		public uint Alpha
		{
			get { return _alpha; }
			set
			{
				_alpha = value;
				if (_image != null)
					_image.Alpha = _alpha;
			}
        }

        /// <summary>
        /// Cleaning.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_image != null)
                    _image.Dispose();
            }
            base.Dispose(disposing);
        }


        #region AlphaControl Members

        /// <summary>
        /// Draws the image if any.
        /// </summary>
        /// <param name="gx"></param>
        public override void Draw(Graphics gx)
		{
			if (_image != null)
				_image.Draw(gx, this.Bounds);
		}

        public AlphaPictureBox()
            : base()
        {
            //this.ParentChanged += new System.EventHandler(AlphaPictureBox_ParentChanged);
            this.EnabledChanged += new System.EventHandler(AlphaPictureBox_EnabledChanged);
        }

        void AlphaPictureBox_EnabledChanged(object sender, System.EventArgs e)
        {
            Refresh();
        }

        /*void AlphaPictureBox_ParentChanged(object sender, System.EventArgs e)
        {
            this.Parent.MouseDown += new System.Windows.Forms.MouseEventHandler(Parent_MouseDown);
            this.Parent.MouseUp += new System.Windows.Forms.MouseEventHandler(Parent_MouseUp);
            this.Parent.MouseMove += new System.Windows.Forms.MouseEventHandler(Parent_MouseMove);
        }

        public new event MouseEventHandler MouseMove;
        public new event EventHandler Click;
        public new event MouseEventHandler MouseUp;
        public new event MouseEventHandler MouseDown;

        void Parent_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (!this.Visible || !this.Enabled || !HitTest(e.X, e.Y))
                return;
            MouseMove(sender, e);
        }

        void Parent_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (!this.Visible || !this.Enabled || !HitTest(e.X, e.Y))
                return;
            MouseUp(sender, e);
            Click(sender, new EventArgs());
        }

        void Parent_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (!this.Visible || !this.Enabled || !HitTest(e.X, e.Y))
                return;
            MouseDown(sender, e);
        }*/
		#endregion
	}
}
